<?php
/**
 * alltosun.com 商店验证凭证记录控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (zhangyh@alltosun.com) $
 * $Date: 2014-11-17 下午6:41:46 $
 * $Id$
*/

class Action
{   
    //声明每页显示的条数
    private $per_page = 10;


    public function test()
    {   
        echo '<pre>';
        print_r(store_verify_vocher_record_helper::get_store_verify_vocher_num());
        echo '</pre>';
    }


    /**
    * 显示核销记录列表
    */
    public function index()
    {
        $page          = Request::getParam('page_no', 1);
        $status        = Request::getParam('status', 1);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        //过滤条件
        $filter           = array();
        $filter['status'] = $status;

        if (isset($search_filter['discount_code']) && $search_filter['discount_code']) {
            $filter['discount_code'] = $search_filter['discount_code'];
        }
        if (isset($search_filter['group_platform_name']) && $search_filter['group_platform_name']) {

            //获取平台名称符合的id值
            $group_platform_list = _model('group_platform')->getList(array(' `group_platform_name` LIKE '=>'%'.$search_filter['group_platform_name'].'%'));
            ;
            $group_platform_ids = array();
            if ($group_platform_list) {
                foreach ($group_platform_list as $k => $v) {
                    array_push($group_platform_ids, $v['id']);
                }
                $filter['group_platform_id'] = $group_platform_ids;
            }

        }
        // 用户名
        if(isset($search_filter['user_name']) && $search_filter['user_name']) {
            $user_ids = user_helper::get_user_ids_by_name($search_filter['user_name']);
            $filter['user_id'] = $user_ids;
        }
        // @TODO 商店名
        if(isset($search_filter['store_name']) && $search_filter['store_name']) {
            
        }
        // @TODO 商品名
        if(isset($search_filter['goods_name']) && $search_filter['goods_name']) {
            
        }
        //优惠码生成时间
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }

        //优惠码兑换时间
        if (isset($search_filter['exchange_start_date']) && $search_filter['exchange_start_date']) {
            $filter['exchange_time >='] = $search_filter['exchange_start_date'].' 00:00:00';
        }
        if (isset($search_filter['exchange_end_date']) && $search_filter['exchange_end_date']) {
            $filter['exchange_time <='] = $search_filter['exchange_end_date'].' 23:59:59';
        }
        //优惠码过期时间
        if (isset($search_filter['expired_start_date']) && $search_filter['expired_start_date']) {
            $filter['expired_time >='] = $search_filter['expired_start_date'].' 00:00:00';
        }
        if (isset($search_filter['exchange_end_date']) && $search_filter['exchange_end_date']) {
            $filter['expired_time <='] = $search_filter['exchange_end_date'].' 23:59:59';
        }

        //分页
        $record_count = _model('store_verify_vocher_record')->getTotal($filter);
        
        $record_list = get_data_list('store_verify_vocher_record', $filter, ' ORDER BY `id` DESC ', $page, $per_page = 20);

        if ($record_list) {
            foreach ($record_list as $k => $v) {
                //查询平台名称
                $platform_info = _uri('group_platform', $v['group_platform_id']);
                $record_list[$k]['group_platform_name'] = $platform_info['group_platform_name'];
                //用户名称,商店名称,商品名称
                $record_list[$k]['user_name'] = user_helper::get_username_by_id($v['user_id']);
                $record_list[$k]['store_name'] = store_helper::get_store($v['store_id'])['title'];
                $record_list[$k]['goods_name'] = order_helper::get_goods_title($v['order_id']);
            }
            Response::assign('record_list', $record_list);
        }

        
        Response::assign('is_advenced', $is_advenced);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/record_list.html');
    }

    /**
    * 伪删除
    */
    public function delete()
    {
        //接收id
        $id  = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            _model('store_verify_vocher_record')->update($v, array('status' => 0));
        }
        return 'ok';
    }

    /**
    * 还原
    */
    public function change_status()
    {
        //接收id
        $id = Request::Get('id', 1);

        _model('store_verify_vocher_record')->update($id, array('status' => 1));

        Response::assign('status', 1);
        Response::redirect(AnUrl('store_verify_vocher_record/admin'));
    }

    /**
    * 详细信息
    */
    public function show_detail()
    {
        $id   = Request::getParam('id', 1);
        $info = _uri('store_verify_vocher_record', $id);

        // 获取平台名称,商品名称,用户名,商品名
        $group_platform_info = _uri('group_platform', $info['group_platform_id']);
        // 商店名 用户名 商品名
        $info['user_name']   = user_helper::get_username_by_id($info['user_id']);
        $info['store_name']  = store_helper::get_store($info['store_id'])['title'];
        $info['goods_name']  = order_helper::get_goods_title($info['order_id']);

        $info['group_platform_name'] = $group_platform_info['group_platform_name'];

        Response::assign('info', $info);

        Response::display('admin/detail.html');
    }

    public function download_list()
    {
        $status        = Request::getParam('status', 1);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装过滤条件
        $filter = array();
        $filter['status'] = $status;
        if (isset($search_filter['discount_code']) && $search_filter['discount_code']) {
            $filter['discount_code'] = $search_filter['discount_code'];
        }

        if (isset($search_filter['group_platform_name']) && $search_filter['group_platform_name']) {

            //获取平台名称符合的id值
            $group_platform_list = _model('group_platform')->getList(array(' `group_platform_name` LIKE '=>'%'.$search_filter['group_platform_name'].'%'));
            ;
            $group_platform_ids = array();
            $group_platform_id_str = '';
            if ($group_platform_list) {
                foreach ($group_platform_list as $k => $v) {
                    array_push($group_platform_ids, $v['id']);
                }
                $filter['group_platform_id'] = $group_platform_ids;
            }
        }

        // @TODO 用户名
        if(isset($search_filter['user_name']) && $search_filter['user_name']) {
            
        }
        // @TODO 商店名
        if(isset($search_filter['store_name']) && $search_filter['store_name']) {
            
        }

        // @TODO 商品名
        if(isset($search_filter['goods_name']) && $search_filter['goods_name']) {
            
        }

        //优惠码生成时间
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }

        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }

        //优惠码生成时间
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }

        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }

        //优惠码兑换时间
        if (isset($search_filter['exchange_start_date']) && $search_filter['exchange_start_date']) {
            $filter['exchange_time >='] = $search_filter['exchange_start_date'].' 00:00:00';
        }

        if (isset($search_filter['exchange_end_date']) && $search_filter['exchange_end_date']) {
            $filter['exchange_time <='] = $search_filter['exchange_end_date'].' 23:59:59';
        }

        //优惠码过期时间
        if (isset($search_filter['expired_start_date']) && $search_filter['expired_start_date']) {
            $filter['expired_time >='] = $search_filter['expired_start_date'].' 00:00:00';
        }
        
        if (isset($search_filter['exchange_end_date']) && $search_filter['exchange_end_date']) {
            $filter['expired_time <='] = $search_filter['exchange_end_date'].' 23:59:59';
        }

        $record_list = array();

        $record_list = _model('store_verify_vocher_record')->getList($filter, array());

        if ($record_list) {
            foreach ($record_list as $k => $v) {
                //查询平台名称
                $platform_info = _uri('group_platform', $v['group_platform_id']);
                $record_list[$k]['group_platform_name'] = $platform_info['group_platform_name'];
                // @TODO 用户名,商店名,商品名
                $record_list[$k]['user_name'] = '';
                $record_list[$k]['store_name'] = '';
                $record_list[$k]['goods_name'] = '';
            }
        }else{
            return false;
        }

        $this->export_excel($record_list);
    }

    /**
     * 用户订单表
     */
    public function export_excel($list = array())
    {
        //初始化excel对象
        $objPHPExcel = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        //设置当前的sheet索引，用于后续的内容操纵
        $objPHPExcel->setActiveSheetIndex(0);
        $objActSheet = $objPHPExcel->getActiveSheet();
        // 哪几列
        $col_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');
        // 第一排的标题
        $col_title_arr = array('优惠码', '核销平台名', '商店名', '商品名', '用户名', '抵价', '优惠码生成时间', '优惠码兑换时间', '优惠码过期时间');
        $col_content_arr = array('discount_code', 'group_platform_name', 'store_name', 'goods_name', 'user_name', 'worth', 'add_time', 'exchange_time', 'expired_time');
        // 设置文档属性
        foreach ($col_arr as $k => $v) {
            //设置4个列的宽度
            if (in_array($v, array('A', 'B', 'C', 'G', 'H', 'I'))) {
                $objActSheet->getColumnDimension($v)->setWidth(40);
            } else {
                $objActSheet->getColumnDimension($v)->setWidth(20);
            }
            //设置单位格内容A1,B1,C1,D1
            $objActSheet->setCellValue($v."1", $col_title_arr[$k]);
            //格式化数字为字符串，避免导致数字默认右对齐单元格
            $objActSheet->getStyle($v)->getNumberFormat()->setFormatCode("@");
            //设置自动换行
            $objActSheet->getStyle($v)->getAlignment()->setWrapText(true);
        }

        // 填充单元格
        $key = 2;
        foreach ($list as $k=>$v) {

            // 先将合并的信息填入excl
            foreach ($col_arr as $k2=>$v2) {
                $place = $v2.$key;
                $col_content = $col_content_arr[$k2];

                $value = '';
                if ($col_content == 'discount_code') {
                    $value = ' '.$v['discount_code'];
                } elseif ($col_content == 'group_platform_name') {
                    $value = ' '.$v['group_platform_name'];
                } elseif ($col_content == 'store_name') {
                    $value = ' '.$v['store_name'];
                } elseif ($col_content == 'goods_name') {
                    $value = $v['goods_name'];
                } elseif ($col_content == 'user_name') {
                    $value = $v['user_name'];
                } elseif ($col_content == 'worth') {
                    $value = $v['worth'];
                } elseif ($col_content == 'add_time') {
                    $value = $v['add_time'];
                } elseif ($col_content == 'exchange_time') {
                    $value = $v['exchange_time'];
                } elseif ($col_content == 'expired_time') {
                    $value = $v['expired_time'];
                }

                $objActSheet->setCellValue($place, $value);
            }
            $key++;
        }

        //设置页面类型
        header("Content-Type: application/vnd.ms-excel");
        //用于下载的文件头
        header("Content-Type: application/force-download");
        header("Content-Type: application/download");
        header("Content-Type: application/octet-stream");

        //$title = trim($catebirthday_gift_recordry_name."表").".xls";
        $title = date('Y-m-d-H-i-s', time())."核销记录表.xls";
        // ff正常，ie不正常，下面是从网站找到的解决方案
        if (preg_match('/MSIE/',$_SERVER['HTTP_USER_AGENT'])) {
            $title = rawurlencode($title);
        }
        header("Content-Disposition: attachment; filename=".$title);
        header("Content-Transfer-Encoding: binary");
        //最后修改时间
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT", time());
        //不使用缓存
        header('Cache-Control: ust-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: public');
        header("Expires: 0");
        header("Pragma: public");
        $objWriter->save('php://output');
        exit;
    }
}

?>